package com.itheima.web.controller.store.catalog;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Catalog;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "CatalogServlet", value = "/store/catalog")
public class CatalogServlet extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            this.list(request, response);
        } else if ("toAdd".equals(operation)) {
            this.toAdd(request, response);
        } else if ("save".equals(operation)) {
            this.save(request, response);
        } else if ("toEdit".equals(operation)) {
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            this.edit(request, response);
        } else if ("delete".equals(operation)) {
            this.delete(request, response);
        }
    }
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //CatalogService catalogService=new CatalogServiceImpl();
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class);
        catalogService.delete(catalog);
        response.sendRedirect(request.getContextPath() + "/store/catalog?operation=list");
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        //CatalogService catalogService= new CatalogServiceImpl();
        catalogService.update(catalog);
        response.sendRedirect(request.getContextPath() + "/store/catalog?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        List<Catalog> all = catalogService.findAll();
        request.setAttribute("courseList",all);
        //CatalogService catalogService = new CatalogServiceImpl();
        Catalog catalog = catalogService.findById(id);
        request.setAttribute("catalog", catalog);
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/update.jsp").forward(request, response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //CatalogService catalogService = new CatalogServiceImpl();
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        Date date = new Date();
        catalog.setCreateTime(date);
        catalogService.save(catalog);
        response.sendRedirect(request.getContextPath() + "/store/catalog?operation=list");
    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Catalog> all = catalogService.findAll();
        request.setAttribute("courseList",all);
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/add.jsp").forward(request, response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //CatalogService catalogService = new CatalogServiceImpl();
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }

        PageInfo all = catalogService.findAll(page, size);
        request.setAttribute("page", all);
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/list.jsp").forward(request, response);
    }
}